/**
 * Copyright (c) Huawei Technologies Co., Ltd. 2021-2021. All rights reserved.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nightonke.boommenusample;

import ohos.agp.components.*;
import ohos.agp.components.element.VectorElement;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import java.util.List;

public class ItemProvider extends BaseItemProvider {

    private final String tag= "ItemProvider";

    private Context context;
    private List<String> list;
    private int layoutResId;

    public ItemProvider(Context contextIn, List<String> listIn, int layoutResIdIn) {
        this.context = contextIn;
        this.list = listIn;
        this.layoutResId = layoutResIdIn;
    }

    /**
     * getCount
     * @return
     */
    @Override
    public int getCount() {
        return list.size();
    }

    /**
     * getItem
     * @param position
     * @return
     */
    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    /**
     * getItemId
     * @param position
     * @return
     */
    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * getComponent
     * @param i
     * @param component
     * @param componentContainer
     * @return
     */
    @Override
    public Component getComponent(int i, Component component, ComponentContainer componentContainer) {
        ProviderHolder holder = null;
        if (component == null) {
            Component cpt = LayoutScatter.getInstance(context).parse(layoutResId, null, false);
            holder = new ProviderHolder(cpt);
            component = cpt;
        } else {
            holder = (ProviderHolder) component.getTag();
        }

        holder.bindData(list.get(i));
        component.setTag(holder);

        return component;
    }

    private class ProviderHolder {
        private Component cpt;
        private Image imgType;
        private Text txtName;
        private Text txtDateTime;
        private Image imgEvent;

        public ProviderHolder(Component component) {
            this.cpt = component;
            this.txtName = (Text) cpt.findComponentById(ResourceTable.Id_txt_name);
        }

        public void bindData(String nameValue) {
            txtName.setText(nameValue);
        }
    }

}


